import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';
import { CleanProductStatus, CleanProductStatusNameList } from '../constants';

/**
 * 保洁服务产品集合
 */
export default (app: Application) => {
  const mongoose = app.mongoose;
  const cleanProductSchema = new mongoose.Schema({
    // 产品名
    productName: {
      type: String,
      required: true,
    },
    // 产品简介
    productDesc: {
      type: String,
      required: true,
    },
    // 产品封面图
    coverImgUrl: {
      type: String,
      required: true,
    },
    // 产品价格
    price: {
      type: String,
      required: true,
    },
    // 产品详细介绍
    details: {
      type: String,
      required: true,
    },
    // 产品状态
    status: {
      type: Number,
      default: CleanProductStatus.offline,
      enum: [ CleanProductStatus.offline, CleanProductStatus.online, CleanProductStatus.deletion ],
    },
  }, { toJSON: { virtuals: true, getters: true }, });

  // 状态描述
  // tslint:disable-next-line:ter-prefer-arrow-callback
  cleanProductSchema.virtual('statusName').get(function (this: any) {
    return CleanProductStatusNameList[this.status];
  });

  cleanProductSchema.plugin(pageFindPlugin, {});
  return mongoose.model('Cleanproduct', cleanProductSchema);
};
